<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="设备号" prop="deviceid">
        <el-input
          v-model="queryParams.deviceid"
          placeholder="设备ID"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否在线 " prop="isonline">
        <el-select v-model="queryParams.isonline" placeholder="请选择是否在线 " clearable size="small">
          <el-option
            v-for="dict in isonlineOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="是否登录" prop="islogined">
        <el-select v-model="queryParams.islogined" placeholder="是否登录" clearable size="small">
          <el-option
            v-for="dict in isloginedOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="wechatid" prop="wechatid">
        <el-input
          v-model="queryParams.wechatid"
          placeholder="wechatid"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['business:accountinfo:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['business:accountinfo:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['business:accountinfo:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="accountinfoList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="设备id" align="center" prop="deviceid" />
      <el-table-column label="操作员昵称" align="center" prop="accountname" />
      <el-table-column label="微信Id" align="center" prop="wechatid" />
      <el-table-column label="微信号" align="center" prop="wechatno" />

      <el-table-column label="是否在线 " align="center" prop="isonline" :formatter="isonlineFormat" />
      <el-table-column label="PC端登录" align="center" prop="islogined" :formatter="isloginedFormat" />

      <el-table-column label="设备昵称" align="center" prop="devnickname" />
      <el-table-column label="手机品牌" align="center" prop="brand" />
      <el-table-column label="手机型号" align="center" prop="module" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNo"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改手机设备状态对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="所属操作员" prop="accountid">
           <el-select v-model="form.accountid" placeholder="操作员">
            <el-option
              v-for="dict in userList"
              :key="dict.userId"
              :label="dict.nickName"
              :value="dict.userId"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="设备ID" prop="deviceid">
          <el-input v-model="form.deviceid" placeholder="请输入设备ID" />
        </el-form-item>
        <el-form-item label="手机别名" prop="devnickname">
          <el-input v-model="form.devnickname" placeholder="请输入手机别名" />
        </el-form-item>
        <el-form-item label="手机型号" prop="module">
          <el-input v-model="form.module" placeholder="请输入手机型号" />
        </el-form-item>
        <el-form-item label="手机品牌" prop="brand">
          <el-input v-model="form.brand" placeholder="请输入手机品牌" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listAccountinfo, getAccountinfo, delAccountinfo, addAccountinfo, updateAccountinfo, exportAccountinfo } from "@/api/business/accountinfo";
import { listUser } from "@/api/system/user";

export default {
  name: "Accountinfo",
  components: {
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 手机设备状态表格数据
      accountinfoList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否在线 字典
      isonlineOptions: [],
      // PC端登录字典
      isloginedOptions: [],
      // 评价信息字典
      userList: [],
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 10,
        wechatnick: null,
        isonline: null,
        islogined: null,
        deviceid: null,
        devnickname: null,
        brand: null,
        module: null,
      },
      userqueryParams: {
        pageNum: 1,
        pageSize: 100,
        userName: undefined,
        phonenumber: undefined,
        status: undefined,
        deptId: undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
    this.getuserlist();
    this.getDicts("is_online").then(response => {
      this.isonlineOptions = response.data;
    });
    this.getDicts("is_pc_online").then(response => {
      this.isloginedOptions = response.data;
    });
  },
  methods: {
    /** 查询手机设备状态列表 */
    getList() {
      this.loading = true;
      listAccountinfo(this.queryParams).then(response => {
        console.log(response)
        this.accountinfoList = response.resultList;
        this.total = response.pageTotal;
        this.loading = false;
      });
    },
    // 是否在线 字典翻译
    isonlineFormat(row, column) {
      return this.selectDictLabel(this.isonlineOptions, row.isonline);
    },
    // PC端登录字典翻译
    isloginedFormat(row, column) {
      return this.selectDictLabel(this.isloginedOptions, row.islogined);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        wechatid: null,
        wechatno: null,
        wechatnick: null,
        gender: null,
        avatar: null,
        country: null,
        province: null,
        city: null,
        isonline: 0,
        islogined: 0,
        deviceid: null,
        cid: null,
        did: null,
        devnickname: null,
        brand: null,
        module: null,
        accountid: null,
        loginTime: null,
        modifyTime: null,
        createTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNo = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加手机设备状态";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getAccountinfo(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改手机设备状态";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateAccountinfo(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addAccountinfo(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    getuserlist() {
      listUser(this.addDateRange(this.userqueryParams, [])).then((response) => {
        console.log(response)
        let list = response.rows
        this.userList = list;
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除手机设备状态编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delAccountinfo(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    }
  }
};
</script>